package models.contract;

import models.UUIDModel;
import models.bill.Voucher;
import models.constants.DeletedStatus;
import models.enums.OwnerIdentityType;
import models.enums.OwnerType;
import models.enums.ReviewedStatus;
import util.common.DateUtil;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 合同信息
 */
@Entity
@Table(name = "co_contracts")
public class Contract extends UUIDModel {

    /**
     * 入住编号
     */
    @Column(name = "code")
    public String code;

    /**
     * 合同编号
     */
    @Column(name = "contract_code")
    public String contractCode;
    /**
     * 合同开始时间
     */
    @Column(name = "begin_at")
    public Date beginAt;

    @Transient
    public String beginAtStr;

    /**
     * 合同结束时间
     */
    @Column(name = "end_at")
    public Date endAt;

    @Transient
    public String endAtStr;

    /**
     * 客户类型 业主,租户
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "owner_type")
    public OwnerType ownerType;


    /**
     * 产证号码
     */
    @Column(name = "certificate_number")
    public String certificateNumber;

    /**
     * 业主姓名
     */
    @Column(name = "owner_name", length = 10)
    public String ownerName;

    /**
     * 业主电话
     */
    @Column(name = "owner_phone", length = 15)
    public String ownerPhone;

    /**
     * 家庭电话
     */
    @Column(name = "family_phone", length = 15)
    public String familyPhone;

    /**
     * 业主身份类型
     * 目前只有身份证
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "owner_identity_type", length = 20)
    public OwnerIdentityType ownerIdentityType;

    /**
     * 业主身份信息
     */
    @Column(name = "owner_identity_code", length = 20)
    public String ownerIdentityCode;

    /**
     * 信息审核状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "reviewed_status", length = 10)
    public ReviewedStatus reviewedStatus;

    /**
     * 月租
     */
    @Column(name = "monthly_rent")
    public BigDecimal monthlyRent;

    /**
     * 物业费
     */
    @Column(name = "property_fee")
    public BigDecimal propertyFee;

    /**
     * 备注信息
     */
    @Column(name = "remark", length = 100)
    public String remark;

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    public static Contract findByContractId(String id) {
        return Contract.find("id = ? and deleted = ?", id, DeletedStatus.UN_DELETED).first();
    }

    /**
     * 检查合同是否被使用(凭证Voucher)
     * @param contractId
     * @return
     */
    public static Boolean checkIsUsedByVoucher(String contractId){
        return Voucher.count("contract.id = ? and deleted = ?" ,contractId , DeletedStatus.UN_DELETED) > 0 ;
    }

    //给定起始时间,判断与合同时间是否包含或者重叠
    public static Boolean checkInContract( Contract contract ,Date beganAt , Date endAt){

        if (contract.beginAt.after(beganAt) && contract.beginAt.before(endAt))
            return true;
        else if (contract.endAt.after(beganAt) && contract.endAt.before(endAt))
            return true;
        else if (contract.beginAt.before(beganAt) && contract.endAt.after(endAt))
            return true;
        else return false;
    }

}
